package com.ztjava.core.listener;

import java.io.File;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.Ret;
import com.xiaoleilu.hutool.io.FileUtil;
import com.xiaoleilu.hutool.io.resource.ResourceUtil;
import com.xiaoleilu.hutool.util.CharsetUtil;
import com.ztjava.core.Constant;
import com.ztjava.core.Jsub;
import com.ztjava.core.tool.ConfigTool;
import com.ztjava.core.utiliy.PathUtil;
import com.ztjava.plugin.event.EventTag;
import com.ztjava.plugin.event.core.ApplicationListener;
import com.ztjava.plugin.event.core.Listener;
import com.ztjava.plugin.event.core.RequestEvent;

@Listener(tag = EventTag.RELOAD_CACHE)
public class ReloadCacheListener implements ApplicationListener<RequestEvent>{

	@Override
	public Ret onApplicationEvent(RequestEvent event) {
		String type = event.getSource().toString();
		if(type.equals("ALL")){
			System.out.println(Jsub.folder+"开始刷新缓存！");
			ConfigTool configTool = new ConfigTool();
			JSONObject server_config = null;
			try {
				server_config = JSONObject.parseObject(FileUtil.readString(PathUtil.getWebPath()+File.separator+Jsub.server_config_name, CharsetUtil.UTF_8));
				
			}catch (Exception e){
				
			}
			
			
			JSONObject app_config = JSONObject.parseObject(ResourceUtil.readUtf8Str(Jsub.app_config_name));
			configTool.parseAllConfig(app_config,server_config);
		}
		
		return Ret.create(Constant.RESULT, Constant.SUCCESS);
	}

}
